package com.yw.atp.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Phome;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.service.PhomeService;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("phomeAction")
public class PhomeAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private PersistenceService service;
	@Autowired
	private PhomeService phomeService;
	HttpServletRequest request;
	public Phome phome;
    public String res;
    
	public String show(){
		phome = phomeService.getPhome();
		return SUCCESS;
	}
	
	public String edit(){
		phome.setVersion(phome.getVersion()+1);
		service.save(phome);
		return SUCCESS;
	}
	
	public String version(){
		try {
			phome = phomeService.getPhome();
			JSONObject jo = new JSONObject();
			jo.put("version",phome.getVersion());
			jo.put("content",phome.getContent());
			res = jo.toString();
			return SUCCESS;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		res = "{}";
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public PhomeService getPhomeService() {
		return phomeService;
	}
	
	public void setPhomeService(PhomeService phomeService) {
		this.phomeService = phomeService;
	}
	
	public Phome getPhome() {
		return phome;
	}
	
	public void setPhome(Phome phome) {
		this.phome = phome;
	}
	
	public PersistenceService getService() {
		return service;
	}
	
	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}
}
